import { Component,OnInit } from '@angular/core';
import {ModuleTree,User} from "../../entities/entities";
import {ActivatedRoute} from "@angular/router";
import {
  FormBuilder,
  FormGroup,
  Validators,
  FormControl
} from '@angular/forms';
/**新增或修改用户*/
@Component({
  selector: 'insert-or-update-user-component',
  templateUrl:`InsertOrUpdateUserComponent.html`,
  styles  : [`InsertOrUpdateUserComponent.scss`]
})
export class InsertOrUpdateUserComponent implements OnInit {
  validateForm: FormGroup;
  user:User;
  _submitForm() {
    for (const i in this.validateForm.controls) {
      this.validateForm.controls[ i ].markAsDirty();
    }
  }

  constructor(private fb: FormBuilder,private route:ActivatedRoute) {
    this.user=new User(null,null,null,null,null,null,null,null,null,null);
    
  }

  updateConfirmValidator() {
    /** wait for refresh value */
    setTimeout(_ => {
      this.validateForm.controls[ 'checkPassword' ].updateValueAndValidity();
    });
  }

  confirmationValidator = (control: FormControl): { [s: string]: boolean } => {
    if (!control.value) {
      return { required: true };
    } else if (control.value !== this.validateForm.controls[ 'password' ].value) {
      return { confirm: true, error: true };
    }
  };

  getCaptcha(e: MouseEvent) {
    e.preventDefault();
  }

  ngOnInit() {
    this.validateForm = this.fb.group({
      account            : [ null, [ Validators.required  ] ],
      password         : [ null, [ Validators.required  ] ],
      checkPassword    : [ null, [ Validators.required, this.confirmationValidator ] ],
      nickname         : [ null, [ Validators.required ] ],
      phoneNumberPrefix: [ '+86' ],
      phoneNumber      : [ null, [ Validators.required ] ],
      website          : [ null, [ Validators.required ] ],
      captcha          : [ null, [ Validators.required ] ],
      agree            : [ false ]
    });
  }

  getFormControl(name) {
    return this.validateForm.controls[ name ];
  }
}